﻿/*
 * Copyright (c) 2012, University of Southampton All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of University of Southampton nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Xml.Schema;
using System.Xml;

namespace LabTroveWebParts
{
    /// <summary>
    /// Class post holds the contents of a LabTrove post, and can be serialized to XML to feed to the REST API.
    /// </summary>
    [Serializable]
    public class post
    {
        /// <summary>
        /// The title of the post
        /// </summary>
        private string _title;
        public string title
        {
            get { return _title; }
            set { _title = value; }
        }

        /// <summary>
        /// The section of the blog in which this appears
        /// </summary>
        private string _section;
        public string section
        {
            get { return _section; }
            set { _section = value; }
        }

        /// <summary>
        /// Specify the username of the author in this field.
        /// </summary>
        private author_type _author;
        public author_type author
        {
            get { return _author; }
            set { _author = value; }
        }

        /// <summary>
        /// The content of the blog post, using bbcode-like markup if necessary.  
        /// </summary>
        private XmlCDataSection _content;
        public XmlCDataSection content
        {
            get
            {
                return _content;
            }

            set
            {
                _content = value;
            }
        }

        /// <summary>
        /// The date stamp for the post.
        /// </summary>
        private DateTime _datestamp;
        public DateTime datestamp
        {
            get { return _datestamp; }
            set { _datestamp = value; }
        }

        /// <summary>
        /// Integer blog ID.  Use this, or the string blog_sname (short name), see
        /// http://www.labtrove.org/documentation/Blog:API_REST
        /// NB: LabTrove seems to ignore this attribute when given a blog_sname
        /// </summary>
        private int _blog_id;
        public int blog_id
        {
            get { return _blog_id; }
            set { _blog_id = value; }
        }

        private int _id;
        public int id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// String short blog name (sname), use this or integer blog_id to identify blog, see
        /// http://www.labtrove.org/documentation/Blog:API_REST
        /// </summary>
        private string _blog_sname;
        public string blog_sname
        {
            get { return _blog_sname; }
            set { _blog_sname = value; }
        }

        /// <summary>
        /// An arbitrary number of key-value pairs may be stored here,
        /// where keys and values are of type 'String'.
        /// </summary>
        private SerializableStringDictionary<String, List<String>> _metadata;
        public SerializableStringDictionary<String, List<String>> metadata
        {
            get { return _metadata; }
            set { _metadata = value; }
        }

        /// <summary>
        /// The attached_data list is a list of integer IDs that refer to data in the
        /// LabTrove system, see http://www.labtrove.org/documentation/Blog:API_REST
        /// </summary>
        private List<data> _attached_data;
        public List<data> attached_data
        {
            get { return _attached_data; }
            set { _attached_data = value; }
        }

        /// <summary>
        /// When editing posts a reason is specified.
        /// </summary>
        private string _edit_reason;
        public string edit_reason
        {
            get { return _edit_reason; }
            set { _edit_reason = value; }
        }

        public post()
        {
            _author = new author_type();
            _metadata = new SerializableStringDictionary<string,List<string>>();
        }
    }

    [Serializable]
    public class data : IXmlSerializable
    {
        private int _dataNum;
        public int dataNumber
        {
            get { return _dataNum; }
            set { _dataNum = value; }
        }

        private String _dataType;
        [XmlAttribute]
        public string type
        {
            get { return _dataType; }
            set { _dataType = value; }
        }

        // Constructors
        public data()
        {
            _dataType = "local";
        }

        public data(int n)
        {
            _dataType = "local";
            _dataNum = n;
        }


        // XML Serialization Infrastructure
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("type", "", "local");
            writer.WriteString(_dataNum.ToString());
        }

        public void ReadXml(XmlReader Reader)
        {
            if (Reader.GetAttribute(0) == "local")
            {
                _dataNum = Reader.ReadElementContentAsInt();
            }
        }

        public XmlSchema GetSchema()
        {
            return (null);
        }
    }

    public class author_type
    {
        private string _username;
        public string username
        {
            get { return _username; }
            set { _username = value; }
        }


        public author_type()
        {
        }

        public author_type(string uname)
        {
            _username = uname;
        }

    }

}
